<?php

/*
 * this is for fetching pms
 */
require_once("../../connection.php");
require_once("../../../functions_shared.php");

//getting the params
$type = $_POST['type'];
$user = $_POST['user']; // the presently logged in user
if(isset($_POST['offset'])) 	
	$offset_text = "AND `id`<".$_POST['offset']."";
else $offset_text = "";

//processing the params
$arr = explode("_",$type);
$type1 = $arr[0];

if($type1 == "pms")
{
	$result = mysql_query("SELECT * FROM site_pms WHERE `type`='$type' AND `del`!='1' ".$offset_text." ORDER BY `id` DESC LIMIT 10");
	if(!$result) echo mysql_error();
	 
}
else if($type1 == "poll")
{
	$timestamp = (int)$_POST['offset'];
	$type2 = "".$arr[1]."_".$arr[2]."_".$arr[3]."";
	

	$result = mysql_query("SELECT * FROM site_pms WHERE `type`='$type2' AND `del`!='1' AND `timestamp`>'$timestamp' ORDER BY `id` DESC LIMIT 10");
	if(!$result) echo mysql_error();
}

 
$size = mysql_numrows($result);
if($size > 0)
$last_id = mysql_result($result, $size-1, 'id');
$output = "";

// option to view more
if( $size == 10 && $type1!="poll") 
	$output=$output.'<div class="a stream_more" onclick=\'init_pms("'.$user.'", "'.$type.'",'.$last_id.')\' id="'.$type.'_more">Earlier</div><br>';

/*
if(mysql_numrows($result) == 0)
$output = $output.' <p>No messages yet! </p>';
 *  
 * removed because when new message comes, this text is still there. coz the new
 * stuff just gets appended to the div and the old materrial is there as it is.
 */


//actual stream
for($i=$size-1;$i>=0;$i--)
{
	$name = mysql_result($result, $i, 'name');
	$timestamp = mysql_result($result, $i, 'timestamp');
	$out = mysql_result($result, $i, 'text');
	$fbid = mysql_result($result, $i, 'fbid');
	$row_id = mysql_result($result, $i, 'id'); 
	
	// if the other person is reading this, set read 
	if($user != $fbid)	
	$temp1 = mysql_query("UPDATE site_pms SET `read`='11' WHERE `id`='$row_id'");
	
	//$output = $output.'<div style="float:left"><img src="http://graph.facebook.com/'.$fbid.'/picture"/></div>';
	//output differently aligned for one person and the other
	if($fbid == $user){
		$output=$output.'<!--<div style="float:left"><img src="http://graph.facebook.com/'.$fbid.'/picture"/></div>
		<div style="float:left"><div class="tri_left"></div></div>--><div style="text-align:left;float:left;width:400px;" class="dialog-left">';
		
	//$output=$output.'<strong style="font-size:80%">'.$name.'</strong><br>';
	$out = stripslashes($out);
	if($out!="")  $output=$output.'<div  style="font-size:10pt">'.add_emoticons(convertUrl($out)).'</div>';
	$output=$output.'<span class="faded time_stream" style="float:right;font-size:80%"  id="time_pms_'.$row_id.'">'.process_time($timestamp).'</span></div><div class="clear"></div><br>';
	}
	else
		{
		$output=$output.'<!--<div style="float:right"><img src="http://graph.facebook.com/'.$fbid.'/picture"/></div>
		<div style="float:right"><div class="tri_right"></div></div>--><div style="float:right;width:400px;text-align:right" class="dialog-right">';
		
	 //$output=$output.'<strong  style="font-size:80%">'.$name.'</strong><br>';
	$out = stripslashes($out);
	if($out!="")  $output=$output.'<div  style="font-size:10pt">'.add_emoticons(convertUrl($out)).'</div>';
	$output=$output.'<span class="faded time_stream" style="float:left;font-size:80%"  id="time_pms_'.$row_id.'">'.process_time($timestamp).'</span></div>';
	 
	$output = $output.'<div class="clear"></div><br>'; 
		} 
	
} 

	$response = array();
	$response['output'] = $output;
	date_default_timezone_set('Asia/Calcutta');
	$response['timestamp'] =  time();
	echo json_encode($response);
 

?>
